<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

/*
 * Booking products
 * author	:	Tran Van Thanh
 * email	:	thanhtran@vietnambiz.com
 * date		:	06/09/2011
 */

require_once 'client'.EXT;

class Booking extends Client {

	function __construct()
	{
		parent::__construct();		
        $this->load->library(array("form_validation","Message","pagination"));
		$this->load->helper(array('custom_string','form'));
		$this->load->model(array('booking_model','home_model','questions_model'));	

		$this->data['support'] 			= $this->home_model->get_support();		
	}
	
	function index()
	{		
		//meta tag
	    $this->data['title'] 			= get_title('[TITLE]',"Đặt hàng liên hệ");
	    $this->data['description'] 		= get_description('[DESCRIPTION]',"Đặt hàng liên hệ");
		$this->data['keywords'] 		= get_keywords('[KEYWORDS]',"Đặt hàng liên hệ");	    
		//end meta tag
		
		$this->data['tpl_file']		=	'booking/index';
		$this->load->view('layout/default',$this->data);
	}
	
	function buyproducts($code_products = null)
	{		
		if ($this->input->server('REQUEST_METHOD') === 'POST')
        {        	
        	$this->form_validation->set_message('required', '%s');
            $this->form_validation->set_message('valid_email', '%s');
            $this->form_validation->set_rules("txtName","Họ tên","required");            
            $this->form_validation->set_rules("txtAddress","Địa chỉ","required");
            $this->form_validation->set_rules("txtEmail","Email","required|valid_email");
            $this->form_validation->set_rules("txtPhone","Điện thoại","required");              
            $this->form_validation->set_rules("txtCodeProducts","Mã sản phẩm","required");
            $this->form_validation->set_rules("txtContent","Nội dung","required");
                                  
            if($this->form_validation->run() === TRUE)
            {            	
            	 $fullname			=	strip_tags($this->input->post('txtName'));                 
            	 $email          	=	strip_tags($this->input->post('txtEmail'));
                 $address			=	strip_tags($this->input->post('txtAddress'));
                 $phone				=	strip_tags($this->input->post('txtPhone'));
                 $code_pro			=	strip_tags($this->input->post('txtCodeProducts'));
                 $content           = 	strip_tags($this->input->post('txtContent'));
                 $id_customer		=  	0;
                         		 	                
                 if($this->booking_model->check_code_products($code_pro)==TRUE)
                 {                 		
	                 if($this->questions_model->check_email_customers($email)==FALSE)
	                 {
		                 $data_customers = array(
		                 	'fullname'		=> $fullname,
		                 	'email'			=> $email,
		                 	'address'		=> $address,
		                 	'telephone'		=> $phone,
		                 	'date_create'	=> date('Y-m-d H:i:s'),
		                 	'active'		=> 'yes'
		                 );
		                 $this->db->insert('customers', $data_customers);
			             $id_customer = $this->db->insert_id();
	                 }
	                 else
	                 {
	                 	$customer_info = $this->questions_model->get_customers_info_by_email($email);
		                $id_customer   = $customer_info->id;  
	                 }
	                 
	                 $id_products 	= $this->booking_model->get_products_info_by_code($code_pro);
	                 
	                 $date_construction		= $this->input->post('date_construction');
	        		 $month_construction	= $this->input->post('month_construction');
	        		 $year_construction		= $this->input->post('year_construction');
	        		 $time_construction		= $year_construction.'-'.$month_construction.'-'.$date_construction;
	        		 
                 	 //Insert data to table booking
                 	 $data = array(
                 	 	'id_customer'		=> $id_customer,
                 	 	'id_product'		=> $id_products->id,
                 	 	'time_construction' => $time_construction,
                 	 	'notes'				=> $content,
                 	 	'date_booking'		=> date('Y-m-d H:i:s'),
                 	 	'status'			=> 'yes'
                 	 );
                 	 $re = $this->db->insert('booking', $data);
                 	 if($re == TRUE)
	                 {
	                 	$this->message->add("success",'<li class=success><p>Bạn đã đặt hàng thành công! Xin cảm ơn!<META HTTP-EQUIV=refresh CONTENT=5;URL="'.base_url().'"></p></li>');
	                 }
                 }
                 else
                 {
                 	$this->message->add("error",'<li class=error><p>Không tìm thấy sản phẩm nào có mã: <b>'.$code_pro.'</b><br>Hãy kiểm tra lại!</p></li>');
                 }
            }
        }
        if(isset($code_products) && $code_products != null)
        {
        	$this->data['code_products_choice']	= $this->booking_model->get_products_info_by_code($code_products);
        }
        
        $this->data['tpl_file']		=	'booking/index';
		$this->load->view('layout/default',$this->data);	
	}
	
	function contacts()
	{
		if ($this->input->server('REQUEST_METHOD') === 'POST')
        {        	
        	$this->form_validation->set_message('required', '%s');
            $this->form_validation->set_message('valid_email', '%s');
            $this->form_validation->set_rules("txtContactName","Họ tên","required");            
            $this->form_validation->set_rules("txtContactAddress","Địa chỉ","required");
            $this->form_validation->set_rules("txtContactEmail","Email","required|valid_email");
            $this->form_validation->set_rules("txtContactPhone","Điện thoại","required");
            $this->form_validation->set_rules("txtContactContent","Nội dung","required");
                                  
            if($this->form_validation->run() === TRUE)
            {            	
            	 $fullname			=	strip_tags($this->input->post('txtContactName'));                 
            	 $email          	=	strip_tags($this->input->post('txtContactEmail'));
                 $address			=	strip_tags($this->input->post('txtContactAddress'));
                 $phone				=	strip_tags($this->input->post('txtContactPhone'));                 
                 $content           = 	strip_tags($this->input->post('txtContactContent'));
                 $id_customer		=  	0;
                         		 	                                                  		
                 if($this->questions_model->check_email_customers($email)==FALSE)
                 {
	                 $data_customers = array(
	                 	'fullname'		=> $fullname,
	                 	'email'			=> $email,
	                 	'address'		=> $address,
	                 	'telephone'		=> $phone,
	                 	'date_create'	=> date('Y-m-d H:i:s'),
	                 	'active'		=> 'yes'
	                 );
	                 $this->db->insert('customers', $data_customers);
		             $id_customer = $this->db->insert_id();
                 }
                 else
                 {
                 	$customer_info = $this->questions_model->get_customers_info_by_email($email);
	                $id_customer   = $customer_info->id;  
                 }
                                         		 
                  //Insert data to table contacts with type = contact
                  $data = array(
                  	'id_customer'		=> $id_customer,                  	
                  	'contents'			=> $content,
                  	'date_create'		=> date('Y-m-d H:i:s')                  	                
                  );
                  $re = $this->db->insert('contacts', $data);
                  if($re == TRUE)
                 {
                 	$this->message->add("success",'<li class=success><p>Bạn đã gửi ý kiến thành công! Xin cảm ơn!<META HTTP-EQUIV=refresh CONTENT=5;URL="'.base_url().'"></p></li>');
                 }                 
            }
        }
		$this->data['tpl_file']		=	'booking/index';
		$this->load->view('layout/default',$this->data);
	}
}